﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Medical_Store_Billing.Component;
using System.Windows.Forms;
using Medical_Store_Billing.Object_Class;

namespace Medical_Store_Billing.Data_Access_Layer
{
    class KhachhangDAL
    {
        Dataservice m_khachhangdal = new Dataservice();

        public DataTable Laydskhach()
        {
            SqlCommand cmd = new SqlCommand("Select * from khachhang");

            m_khachhangdal.Load(cmd);

            return m_khachhangdal;
        }
        public DataTable lay()
        {
            SqlCommand cmd = new SqlCommand("Select * from khachhang");

            m_khachhangdal.Load(cmd);

            return m_khachhangdal;
        }
        public DataTable Timkh(string makh)
        {
            SqlCommand cmd = new SqlCommand("Select * from khachhang where makh like N'%" + makh + "%'");
            m_khachhangdal.Load(cmd);
            return m_khachhangdal;
        }
        public bool Themkh(Khachhangclass m_kh)
        {
            SqlCommand cmd = new SqlCommand("Insert into khachhang values(@makh,@tenkh,@diachi,@sodt,@dothanthiet)");
            cmd.Parameters.Add("makh", SqlDbType.VarChar).Value = m_kh.Makh;
            cmd.Parameters.Add("tenkh", SqlDbType.NVarChar).Value = m_kh.Tenkh;
            cmd.Parameters.Add("diachi", SqlDbType.VarChar).Value = m_kh.Diachi;
            cmd.Parameters.Add("sodt", SqlDbType.VarChar).Value = m_kh.Sodt;
            cmd.Parameters.Add("dothanthiet", SqlDbType.Int).Value = m_kh.Dothanthiet;

            if (m_khachhangdal.ExecuteNonQuery(cmd) > 0)
            {
                MessageBox.Show("Thêm Khách Hàng Thanh Cong!");
                return true;
            }
            else return false;
        }

        public bool Suakh(Khachhangclass m_kh)
        {
            SqlCommand cmd = new SqlCommand("Update khachhang set tenkh = @tenkh, diachi = @diachi, sodt = @sodt, dothanthiet = @dothanthiet where makh = @makh");
            cmd.Parameters.Add("makh", SqlDbType.VarChar).Value = m_kh.Makh;
            cmd.Parameters.Add("tenkh", SqlDbType.NVarChar).Value = m_kh.Tenkh;
            cmd.Parameters.Add("diachi", SqlDbType.VarChar).Value = m_kh.Diachi;
            cmd.Parameters.Add("sodt", SqlDbType.VarChar).Value = m_kh.Sodt;
            cmd.Parameters.Add("dothanthiet", SqlDbType.Int).Value = m_kh.Dothanthiet;

            if (m_khachhangdal.ExecuteNonQuery(cmd) > 0)
            {
                MessageBox.Show("Sửa Khách Hàng thành công!");
                return true;
            }
            else return false;
        }
    }
}
